var jq=$.noConflict();
var app1 = angular.module('myApp',['ngResource','ngCookies']);
app1.controller('loginCtr',['$scope','$cookieStore','$resource', function ($scope,$cookieStore,$resource) {
    $scope.postData = {
        "loginname": "",
        "passwords": ""
    };
    $scope.errinfo = '';
    $scope.passtype='password';
    $scope.passchange=function () {
       if($scope.passtype==='password'){
           $scope.passtype='text';
       }
       else{
           $scope.passtype='password';
       }
    };
    $scope.loginStore={};
    var callServive={
        callres:function (content) {
            window.clearInterval(ii);
            jq("#process").css({"width": "100%"});
            jq("#process").fadeOut(1000);
            if(content.dataCount>0){
                $scope.loginStore.name=content.data[0].name;
                $scope.loginStore.empid=content.data[0].employeeID;
                $scope.loginStore.usertype=content.data[0].userType;
                $scope.loginStore.username = content.data[0].loginName;
                $cookieStore.put("loginInfo",$scope.loginStore);
                if($scope.loginStore.usertype===1){
                    window.location.href="/teacher/Index";
                }
                else if($scope.loginStore.usertype===2){
                    window.location.href="/student/Index";
                }

            }
            else if(content.isSuccess&&content.dataCount===null){
                document.getElementById("err").style.opacity = 1;
                $scope.errinfo = "请检查您的用户名密码！";
            }
            else if (content.status == 500 || content.status == 502 || content.status == 503) {
                document.getElementById("err").style.opacity = 1;
                $scope.errinfo = "连接服务器出错了，刷新试试吧！";
            }
        }
    };
    //注册回车按钮事件
    document.onkeydown=function (e) {
        if(e.keyCode==13){
            $scope.login();
        }
    };
    $scope.login = function () {
        if ($scope.postData.loginname == "" && $scope.postData.passwords != "") {
            document.getElementById("err").style.opacity = 1;
            $scope.errinfo = '请输入用户名！';
        }
        else if ($scope.postData.loginname != "" && $scope.postData.passwords == "") {
            document.getElementById("err").style.opacity = 1;
            $scope.errinfo = '请输入密码！';
        }
        else if ($scope.postData.loginname == "" && $scope.postData.passwords == "") {
            document.getElementById("err").style.opacity = 1;
            $scope.errinfo = '请输入用户名和密码！';
        }
        else {
            jq("#process").css({"display": "block", "width": "0%"});
            ii = window.setInterval(aa, 100);
            httpServer.login($resource, $scope.postData, callServive.callres);
        }
    };
}]);

var i = 0;
var ii;
function aa() {
    jq("#process").css({"width": i + "%"});
    i += 10;
    if (i >= 100) {
        window.clearInterval(ii);
        jq("#process").fadeOut(200);
        i = 0;
    }
}

